package org.yx.service.sqcm.view_battery_group_formula;

import com.xiaoleilu.hutool.util.StrUtil;
import org.dbist.dml.Query;
import org.yx.exception.BizException;
import org.yx.models.SbasMatDef;
import org.yx.models.SbasProdMat;
import org.yx.models.SqcmLveDat;
import org.yx.models.SqcmLveDef;
import org.yx.utils.BaseService;
import org.yx.utils.FieldMap;

/**
 * 查询电池的分档方案
 *
 * @author zxw
 * @since 2023/04/11
 **/
public class SqcmViewBatteryGroupFormulaService extends BaseService {
    public SqcmViewBatteryGroupFormulaOut viewBatteryGroupFormula(SqcmViewBatteryGroupFormulaIn in) throws Exception {
        checkStringNotBlank(in, "matCode");
        Query query = new Query(0, 1);
        query.addFilter("orgCode", in.getUserOrgCode());
        query.addFilter("matCode", in.getMatCode());
        if (StrUtil.isNotBlank(in.getLevelVer())) {
            query.addFilter("levelVer", in.getLevelVer());
        }
        query.addOrder("updateDate", false);
        SqcmLveDef levelDef = selectOne(SqcmLveDef.class, query);
        if (levelDef == null) {
            return new SqcmViewBatteryGroupFormulaOut();
            //throw new BizException("SQCM-0010", "产品${matCode}的分档方案不存在。", FieldMap.of("matCode", in.getMatCode()));
        }

        query = new Query();
        query.addFilter("matCode", levelDef.getMatCode());
        query.addFilter("orgCode", levelDef.getOrgCode());
        query.addFilter("matStatus", "N");
        SbasProdMat prodMat = selectOne(SbasProdMat.class, query);

        SqcmViewBatteryGroupFormulaOut out = new SqcmViewBatteryGroupFormulaOut();
        copyProperties(levelDef, out);
        out.setMatDesc(prodMat.getMatDesc());

        query.removeFilter("matStatus");
        query.addFilter("levelVer", levelDef.getLevelVer());
        query.addOrder("levelNo", true);
        out.setLevelList(selectList(SqcmLveDat.class, query));

        return out;
    }
}